package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.vo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import lombok.val;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;
    public PageResult<Brand> queryBrandByPage(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
                //分页
                PageHelper.startPage(page,rows);


                //过滤
                Example example = new Example(Brand.class);
                //定义编写条件
                if(StringUtils.isNotBlank(key)){
                    //拼接查询条件
                    example.createCriteria()
                            .orLike("name","%"+key+"%")
                            .orEqualTo("letter",key.toUpperCase());
                }
                //排序
                if(StringUtils.isNotBlank(sortBy)){
                    //拼接 排序
                    String orderByClause = sortBy +(desc?" DESC":" ASC");
                    example.setOrderByClause(orderByClause);
                }

                //查询
                 List<Brand> list = brandMapper.selectByExample(example);
               //判断查询结果是否为空
                if(CollectionUtils.isEmpty(list)){
                    //如果为空则抛出异常。
                    throw  new LyException(ExceptionEnum.BRAND_NOT_FOUND);
                }
        //解析分页结果
       PageInfo<Brand> info =  new PageInfo(list);
                //返回结果
        return  new PageResult<Brand>(info.getTotal(),list);
    }

    /**
     * 新增品牌
     * @param brand
     * @param cids
     */
    @Transactional
    public void saveBrand(Brand brand, List<Long> cids) {
            //新增品牌
            brand.setId(null);
           int count =  brandMapper.insert(brand);
           //判断是否新增成功
            if(count != 1){
                throw new LyException(ExceptionEnum.BRAND_SAVE_ERROR);
            }

            //添加关系。
        for (Long cid : cids) {
           count =  brandMapper.insertCategoryBrand(cid,brand.getId());
            if(count != 1){
                throw new LyException(ExceptionEnum.BRAND_SAVE_ERROR);
            }
        }


    }

    public Brand getBrandById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if(brand == null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        return  brand;
    }

    public void delBrandById(Long id) {

        int i = brandMapper.deleteByPrimaryKey(id);

        if(i != 1){
            throw new LyException(ExceptionEnum.BRAND_DELETE_ERROR);
        }

    }

    public Brand queryById(Long brandId) {
        Brand brand = brandMapper.selectByPrimaryKey(brandId);
        if(brand == null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        return  brand;
    }

    public List<Brand> getBrandByCid(Long cid) {
        List<Brand> brands = brandMapper.queryByCategoryId(cid);
        if(CollectionUtils.isEmpty(brands)){
            //如果为空则抛出异常。
            throw  new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brands;
    }

    public List<Brand> queryByIds(List<Long> ids) {
        List<Brand> brandList = brandMapper.selectByIdList(ids);
        if(CollectionUtils.isEmpty(brandList)){
            //如果为空则抛出异常。
            throw  new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        return brandList;
    }
}
